using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Utilities.Menu
{

    public class IntroScreen : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch sprite_batch;

        DrawableGameComponent next_component;
        Texture2D logo;
        Rectangle texture_rectangle;
        Matrix screen_transform;
        float alpha;
        GraphicsDeviceManager graphics;

        #region Constructors

        public IntroScreen(Game game, string logo_path, DrawableGameComponent next_component, GraphicsDeviceManager graphics)
            : base(game)
        {
            alpha = 0.0f;
            logo = game.Content.Load<Texture2D>(logo_path);
            this.next_component = next_component;
            this.graphics = graphics;
            texture_rectangle = new Rectangle(game.GraphicsDevice.Viewport.Width / 2 - logo.Width / 2, game.GraphicsDevice.Viewport.Height / 2 - logo.Height / 2, logo.Width, logo.Height);
        }

        private IntroScreen(Game game, DrawableGameComponent next_component, GraphicsDeviceManager graphics)
            : base(game)
        {
            alpha = 0.0f;
            this.next_component = next_component;
            this.graphics = graphics;
        }

        public IntroScreen(Game game, string logo_path, DrawableGameComponent next_component, Vector2 position, GraphicsDeviceManager graphics)
            : this(game, next_component, graphics)
        {
            logo = game.Content.Load<Texture2D>(logo_path);

            texture_rectangle = new Rectangle((int)position.X, (int)position.Y, logo.Width, logo.Height);
        }

        public IntroScreen(Game game, string logo_path, DrawableGameComponent next_component, Rectangle texture_rectangle, GraphicsDeviceManager graphics)
            : this(game, logo_path, next_component, graphics)
        {
            this.texture_rectangle = texture_rectangle;
        }

        #endregion

        public override void Initialize()
        {
            sprite_batch = (SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
            screen_transform = Game.ScreenTransformMatrix(graphics);

            base.Initialize();
        }

        protected override void LoadContent()
        {
            base.LoadContent();
        }

        bool hasReachValue1 = false;
        public override void Update(GameTime gameTime)
        {
            float dt = (float)gameTime.ElapsedGameTime.TotalSeconds;

            if (!hasReachValue1)
            {
                if (alpha > 1.5f)
                {
                    hasReachValue1 = true;
                }

                alpha += dt;
            }
            else
            {
                if (alpha < 0.0f)
                {
                    Game.Components.Add(next_component);
                    Game.Components.Remove(this);
                }

                alpha -= dt;
            }

            base.Update(gameTime);
        }

        public override void Draw(GameTime gameTime)
        {
            sprite_batch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, screen_transform);

            sprite_batch.Draw(logo, texture_rectangle, Color.White * alpha);

            sprite_batch.End();

            base.Draw(gameTime);
        }
    }
}
